package com.xsh.gls.service.impl;

import com.xsh.gls.mapper.CustomerMapper;
import com.xsh.gls.mapper.SalesmanMapper;
import com.xsh.gls.mapper.UserMapper;
import com.xsh.gls.pojo.Customer;
import com.xsh.gls.pojo.Salesman;
import com.xsh.gls.pojo.User;
import com.xsh.gls.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SalesmanMapper salesmanMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public boolean insert(User user, Salesman salesman) {
        if (salesmanMapper.insertSelective(salesman) == 1) {
            Integer salid = salesmanMapper.selectNewId();
            user.setSalid(salid);
            if (userMapper.insertSelective(user) == 1) {
                return true;
            }
        }
        return false;
    }

    @Override
    public User queryUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> selectByTypeID(Integer typeid) {
        return userMapper.selectByTypeID(typeid);
    }

    @Override
    public User queryUserByUserName(String userName) throws Exception {
        return userMapper.selectByUserName(userName);
    }

    @Override
    public int updateByPrimaryKey(User user) {
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public int updateByPrimaryKeySelective(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User queryUserByPhone(String phone) throws Exception {
        return userMapper.selectByPhone(phone);
    }

    @Override
    public boolean add(User user) throws Exception {
        if (userMapper.insertSelective(user) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public List<User> queryAll(String userName, String sAddress, Integer typeId) throws Exception {
        return userMapper.selectAll(userName, sAddress, typeId);
    }

    @Override
    public boolean updatePwdById(String password, Integer id) {
        if (userMapper.updatePwdById(password, id) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public User selectUserById(Integer id) throws Exception {
        return userMapper.selectUserById(id);
    }

    @Override
    public User selectByUserNameOld(String userName, String oldUserName) {
        return userMapper.selectByUserNameOld(userName, oldUserName);
    }

    @Override
    public User selectByPhoneOld(String phone, String oldPhone) {
        return userMapper.selectByPhoneOld(phone, oldPhone);
    }

    @Override
    public boolean updateById(User user, Salesman salesman) throws Exception {

            if(user.getSalid()!=null) {
                if (salesmanMapper.updateByPrimaryKeySelective(salesman) == 1) {
                    if (userMapper.updateByPrimaryKeySelective(user) == 1) {
                    return true;
                }
            }
        }else{
                if(salesmanMapper.insertSelective(salesman)==1){
                   user.setSalid( salesmanMapper.selectNewId());;
                    if (userMapper.updateByPrimaryKeySelective(user) == 1) {
                        return true;
                    }
                }
            }
        return false;
    }

    @Override
    public boolean delUser(Integer id) throws Exception {
        if (userMapper.updateTypeById(id) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updatePhone1(User user, Salesman salesman, int stausId) throws Exception {

            if (stausId == 0) {
                if (salesmanMapper.insertSelective(salesman) == 1) {
                   user.setSalid( salesmanMapper.selectNewId());
                    if (userMapper.updateByPrimaryKey(user) == 1) {
                    return true;
                    }
                } else {
                    return false;
                }
            } else {
                salesman.setId(stausId);
                if (salesmanMapper.updateByPrimaryKeySelective(salesman) == 1) {
                    if (userMapper.updateByPrimaryKey(user) == 1) {
                        return true;
                    }
                } else {
                    return true;
                }
            }

        return false;
    }

    @Override
    public boolean updatePhone2(User user, Customer customer, int stausId) throws Exception {

        if (stausId == 0) {
            if (customerMapper.insertSelective(customer) == 1) {
               user.setCusid(customerMapper.selectNewId());
                if (userMapper.updateByPrimaryKey(user) == 1) {
                    return true;
                }
            } else {
                return false;
            }
        } else {
            customer.setId(stausId);
            if (customerMapper.updateByPrimaryKeySelective(customer) == 1) {
                if (userMapper.updateByPrimaryKey(user) == 1) {
                    return true;
                }
            } else {
                return true;
            }

        }
        return false;
    }


}
